package edu.sias.course.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import edu.sias.course.domain.pojo.CourseContent;
import edu.sias.course.mapper.CourseContentMapper;
import edu.sias.course.service.CatalogService;
import edu.sias.course.service.ICourseContentService;
import edu.sias.course.utils.Esinterutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 课程内容Service业务层处理
 *
 * @author 若依
 * @date 2023-10-10
 */
@Service
public class CourseContentServiceImpl implements ICourseContentService {

    @Autowired
    private CourseContentMapper courseContentMapper;
    @Autowired
    private Esinterutils esinterutils;
    @Autowired
    private CatalogService catalogService;
    /**
     * 查询课程内容
     */
    @Override
    public CourseContent getCourseContentById(Long id) throws IOException {
        Map<String, Object> byId = esinterutils.getById(String.valueOf(id));

       CourseContent courseContent = new CourseContent();
       courseContent.setId(Long.valueOf(byId.get("id").toString()));
       courseContent.setTitle(byId.get("title").toString());
       courseContent.setContent(byId.get("content").toString());
        return courseContent;
    }

    /**
     * 查询课程内容列表
     */
    @Override
    public List<CourseContent> listCourseContent(CourseContent courseContent) {
        return courseContentMapper.selectCourseContentList(courseContent);
    }

    /**
     * 新增课程内容
     *
     * @return
     */
    @Override
    @Transactional
    public int addCourseContent(CourseContent courseContent) throws IOException {
        courseContentMapper.insertCourseContent(courseContent);
        Long i = courseContent.getId();

        esinterutils.add(String.valueOf(i), courseContent.getTitle(),courseContent.getContent());

        return 0;
    }

    /**
     * 修改课程内容
     */
    @Override
    @Transactional
    public int updateCourseContent(CourseContent courseContent) throws IOException {
        Long i = courseContent.getId();
        esinterutils.deleteById(String.valueOf(i));
        esinterutils.add(String.valueOf(i), courseContent.getTitle(),courseContent.getContent());
        return courseContentMapper.updateCourseContent(courseContent);
    }

    /**
     * 删除课程内容
     */
    @Override
    @Transactional
    public int removeCourseContentById(Long id) throws IOException {
        esinterutils.deleteById(String.valueOf(id));
        return courseContentMapper.deleteCourseContentById(id);
    }
}
